	.data
prompt:     .asciiz  "Enter a floating-point value:\n"
sum:        .asciiz  "Sum: "
newline:    .asciiz  "\n"
test:       .asciiz "test\n"
exponent:   .word    0x7F800000	   #all 1's in exponent field, 0 elsewhere
fraction:   .word    0x007FFFFF	   #all 1's in fraction field, 0 elsewhere
leadingOne: .word    0x00800000    #all zeros expect for a 1 in the position before the fraction field
sign:       .word    0x80000000 

	.text
main:

#lw $t1, fraction

#prompts for first number
li $v0, 4
la $a0, prompt
syscall

li $v0, 6
syscall
mov.s $f1, $f0	# $f1 is the first floating point number

#prompts for second number
li $v0, 4
la $a0, prompt
syscall

li $v0, 6
syscall
mov.s $f2, $f0	# $f2 is the second floating point number


lw $t0, exponent	#used to isolate exponent field
#isolate exponent field of $f1(first input) and put result in $t2
mfc1 $t1, $f1
and $t2, $t1, $t0
srl $t2, $t2, 23

#isolate exponent field of $f2(second input) and put result in $t4
mfc1 $t3, $f2
and $t4, $t3, $t0
srl $t4, $t4, 23


lw $t0, fraction	#used to isolate fraction field
#isolate fraction of first input
and $t6, $t1, $t0
#isolate fraction of second input
and $t7, $t3, $t0


lw $t0, leadingOne	#used to add the implicit leading one to fraction
#add leading(implicit) one to first input
or $t6, $t6, $t0
#add leading(implicit) one to second input
or $t7, $t7, $t0


lw $t0, sign		#used to isolate sign bit
#isolates sign bit of first number and shifts it all the way to the right
and $t1, $t1, $t0
srl $t1, $t1, 31
#isolates sign bit of second number and shifts it all the way to the right
and $t3, $t3, $t0
srl $t3, $t3, 31


move $t5, $t4


#compares exponents and determines how much fraction needs to be shifted
#shift amount is stored in $t5
beq $t2, $t4, ExponentsEqual

	#shifts mantissa of number two if its exponent is smaller
	ShiftExpTwo:
		blt $t2, $t4, ShiftExpOne

		addi $t4, $t4, 1	#increments exponent
		srl $t7, $t7, 1 	#shifts mantissa right
	
		move $t5, $t2


		j ShiftExpTwo
	#shifts mantissa of number one if its exponent is smaller
	ShiftExpOne:
		ble $t4, $t2, ExponentsEqual

		addi $t2, $t2, 1	#increments exponent
		srl $t6, $t6, 1		#shifts mantissa right

		move $t5, $t4

		j ShiftExpOne
ExponentsEqual:



add $t0, $t1, $t3	#adds sign bits, if its equal to 1 then they have mixed signs
beq $t0, 1, MixedSigns
	add $t4, $t6, $t7	#adds mantissa's if they have the same signs
	sll $t1, $t1,  31
	j IncrExp
#adds mantissa's if they have mixed signs
MixedSigns:
	blt $t6, $t7, InputTwoBigger
		sub $t4, $t6, $t7
		beq $t1, 0, MakePositive
		beq $t1, 1, MakeNegative
		
	InputTwoBigger:
		sub $t4, $t7, $t6
		beq $t3, 0, MakePositive
		beq $t3, 1, MakeNegative

MakePositive:
li $t1, 0
sll $t1, $t1, 31
j IncrExp
MakeNegative:
li $t1, 1
sll $t1, $t1, 31



#This normalizes the sum by shifting right and increment exponent
# or shifting left and decrementing exponent
IncrExp:
	blt $t4, 16777216, DecrExp

	srl $t4, $t4, 1 
	addi $t5, $t5, 1
	j IncrExp
DecrExp:
	bge $t4, 8388608, Normalized 

	sll $t4, $t4, 1
	subi $t5, $t5, 1
	j DecrExp

Normalized:


#removes leading one
lw $t0, fraction
and $t4, $t4, $t0

#shifts exponent back to its proper place
sll $t5, $t5, 23

#puts the different parts of the floating point number back together
or $t6, $t5, $t4
or $t6, $t6, $t1

#move the number back to a floating point register
mtc1 $t6, $f1

#prints result
li $v0, 2
mov.s $f12, $f1
syscall



